﻿module fs.Base
module Drawing =
    open System.Drawing
    let font (familyName: string) (emSize: float32) (style: FontStyle) = new Font(familyName, emSize, style)
    module Image =
        let fromStream stream = Image.FromStream stream
        open System.Drawing
        open System.Drawing.Imaging
        open System.Drawing.Drawing2D
        open System.IO
        let watermarkText (picture: Graphics) watermarkText size width (height: float32) =
//                let sizes = [ 16; 14; 12; 10; 8; 6; 4 ]
//                let mutable crFont = null
//                let mutable crSize = new SizeF()
//                let mutable crFont1 = null
//                let mutable crSize1 = new SizeF()
//                for i in sizes do
//                    crFont1 <- font "IntHrP60DlTt" (float32 i) FontStyle.Bold
//                    crSize1 <- picture.MeasureString(watermarkText, crFont1)
//                    if crSize1.Width < width then
//                        crFont <- crFont1
//                        crSize <- crSize1
//                let crFont = font "3 of 9 Barcode" 40.0f FontStyle.Regular
            let crFont = font "c39hrp24dhtt" size FontStyle.Regular
            let crSize = picture.MeasureString (watermarkText, crFont)
//                let xpos = width * 0.01f + crSize.Width / 2.0f
//                let ypos = height * 0.01f
            let xpos = width * 0.9f - crSize.Width / 2.0f
            let ypos = height * 0.99f - crSize.Height
            let StrFormat = new StringFormat(Alignment = StringAlignment.Center)
//                let semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0))
//                picture.DrawString(watermarkText, crFont, semiTransBrush2, xpos + 1.0f, ypos + 1.0f, StrFormat)
//                let semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255))
//                picture.DrawString(watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat)
            let semiTransBrush = new SolidBrush (Color.Black)
            picture.DrawString(watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat)
//                semiTransBrush2.Dispose()
            semiTransBrush.Dispose()
        let watermark (image: Image) text =
            let b = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb)
            let g = Graphics.FromImage(b)
            g.Clear(Color.White)
            g.SmoothingMode <- SmoothingMode.HighQuality
            g.InterpolationMode <- InterpolationMode.High
            g.DrawImage(image, 0, 0, image.Width, image.Height)
            watermarkText g text (float32 image.Width / 20.0f) (float32 image.Width) (float32 image.Height)
            image.Dispose()
            b
        let resize (s: Stream) weight height =
            let i = Image.FromStream(s, true)
            let b = new Bitmap(i, weight, height)
            b.SetResolution(300.0f, 300.0f)
            b
        let fromImage stream = Image.FromStream stream
        let fromFile file = Image.FromFile file
module File=
    module Png =
        open Drawing.Image
        let watermark file text =
            let image = fromFile file
            let r = Drawing.Image.watermark image text
            r.Save file
